package com.eascs.o2o.common.dao;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.jdbc.core.JdbcTemplate;

import com.eascs.app.dao.JdbcSession;
import com.eascs.app.dao.PageResult;
import com.eascs.app.dao.ResultSets;
import com.eascs.app.dao.SqlPageQuery;
import com.eascs.app.dao.StoreProcedure;
import com.eascs.app.dao.TableData;
import com.eascs.app.log.Logger;
import com.eascs.app.log.LoggerFactory;

public abstract class BaseDao {
	protected Logger logger = LoggerFactory.getLogger(this.getClass());
	@Autowired
	@Qualifier("wxSession")
	protected JdbcSession wxSession;

	public <T> T getByIdWithEntity(T entity) {
		return wxSession.getByIdWithEntity(entity);
	}

	public <T> int deleteByIdWithEntity(T entity) {
		return wxSession.deleteByIdWithEntity(entity);
	}

	public <T> T getById(Class<T> entityClass, Object... id) {
		return wxSession.getById(entityClass, id);
	}

	public <T> int deleteById(Class<T> entityClass, Object... id) {
		return wxSession.deleteById(entityClass, id);
	}

	/**
	 * 一定要判断 effectRow，详情见 workflowJdbcSession.insert(entity)
	 * @param entity
	 * @return
	 */
	public <T> T insertWithReturn(T entity) {
		int effectRow = wxSession.insert(entity);
		if (effectRow <= 0){
			return null;
		}
		return entity;
	}
	
	public <T> boolean insert(T entity) {
		return wxSession.insert(entity) >= 1 ? true : false;
	}

	public <T> boolean updateById(T entity) {
		return wxSession.updateById(entity) >= 1 ? true : false;
	}

	public <T> List<T> findEntitys(String sql, Class<T> requiredType, Object... params) {
		return wxSession.findEntitys(sql, requiredType, params);
	}

	public <T> T findEntity(String sql, Class<T> requiredType, Object... params) {
		return wxSession.findEntity(sql, requiredType, params);
	}

	public <T> List<T> findSimpleObjectList(String sql, Class<T> requiredType, Object... params) {
		return wxSession.findSimpleObjectList(sql, requiredType, params);
	}

	public <T> T findSimpleObject(String sql, Class<T> requiredType, Object... params) {
		return wxSession.findSimpleObject(sql, requiredType, params);
	}

	public Integer findInteger(String sql, Object... params) {
		return wxSession.findInteger(sql, params);
	}

	public String findString(String sql, Object... params) {
		return wxSession.findString(sql, params);
	}

	public TableData findTableData(String sql, Object... params) {
		return wxSession.findTableData(sql, params);
	}

	public List<Object[]> findObjectsList(String sql, Object... params) {
		return wxSession.findObjectsList(sql, params);
	}

	public boolean execute(String sql, Object... params) {
		return wxSession.execute(sql, params) >= 1 ? true : false;
	}

	public int[] batchExecute(String sql, List<Object[]> params) {
		return wxSession.batchExecute(sql, params);
	}

	public ResultSets callSP(StoreProcedure storeProcedure) {
		return wxSession.callSP(storeProcedure);
	}

	public String insertWithReturnKey(String insertSql, Object... params) {
		return wxSession.insertWithReturnKey(insertSql, params);
	}

	public String getSql(String sql) {
		return wxSession.getSql(sql);
	}

	public <T> PageResult<T> findDataWithPage(SqlPageQuery<T> sqlPageQuery) {
		return wxSession.findDataWithPage(sqlPageQuery);
	}

	public JdbcTemplate getJdbcTemplate() {
		return wxSession.getJdbcTemplate();
	}

}
